package hr.fer.tel.adhoc.c2d;

import org.json.JSONException;
import org.json.JSONObject;

import adhoc.tel.fer.hr.MainService;
import adhoc.tel.fer.hr.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import android.provider.Settings.Secure;
import android.util.Log;

public class C2DMRegistrationReceiver extends BroadcastReceiver {
	
	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		Log.w("C2DM", "Registration Receiver called");
		if ("com.google.android.c2dm.intent.REGISTRATION".equals(action)) {
			Log.w("C2DM", "Received registration ID");
			final String registrationId = intent
					.getStringExtra("registration_id");
			String error = intent.getStringExtra("error");

			Log.d("C2DM", "dmControl: registrationId = " + registrationId
					+ ", error = " + error);
			String deviceId = Secure.getString(context.getContentResolver(),
					Secure.ANDROID_ID);
			
//			createNotification(context, registrationId);
			
			sendRegistrationIdToServer(deviceId, registrationId);
			
			// Also save it in the preference to be able to show it later
			saveRegistrationId(context, registrationId);
			
			// Start background service
			Intent backgroundService = new Intent(context, MainService.class);
			context.startService(backgroundService);
		}
	}

	private void saveRegistrationId(Context context, String registrationId) {
		PreferencesHelper.initialize(context);
		PreferencesHelper.setC2DRegistrationID(registrationId);
	}

	public void createNotification(Context context, String registrationId) {
		NotificationManager notificationManager = (NotificationManager) context
				.getSystemService(Context.NOTIFICATION_SERVICE);
		Notification notification = new Notification(R.drawable.icon,
				"Registration successful", System.currentTimeMillis());
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

//		Intent intent = new Intent(context, RegistrationResultActivity.class);
//		intent.putExtra("registration_id", registrationId);
//		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
//				intent, 0);
//		notification.setLatestEventInfo(context, "Registration",
//				"Successfully registered", null);
		notificationManager.notify(0, notification);
	}

	// Incorrect usage as the receiver may be canceled at any time
	// do this in an service and in an own thread
	public void sendRegistrationIdToServer(String deviceId,
			String registrationId) {
		Log.d("C2DM", "Sending registration ID to my application server");
		
		String id = adhoc.tel.fer.hr.PreferencesHelper.getFacebookID();
		String name = adhoc.tel.fer.hr.PreferencesHelper.getFacebookName();
		String accessToken = adhoc.tel.fer.hr.PreferencesHelper.getAccessToken();
		
		try {
        	JSONObject userKey = new JSONObject();
        	userKey.put("fb_id", id);
        	userKey.put("name", name);
        	userKey.put("google_c2d", registrationId);
        	userKey.put("accessToken", accessToken);
        	
            Log.d("SENT KEY", registrationId);
            Log.d("SENT ID", id );
            Log.d("SENT NAME", name );
            Log.d("SENT token", accessToken);
            
//            TODO: Send C2D key to server
//            Util.sendToServerByPost("init", userKey);

		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	}
}
